package com.changgou.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.comment.dao.CommentDao;
import com.changgou.comment.service.CommentService;
import com.changgou.entity.Result;
import com.changgou.feign.SkuFeign;
import com.changgou.feign.UserFeign;
import com.changgou.pojo.Comment;
import com.changgou.pojo.Sku;
import com.changgou.pojo.User;
import com.changgou.util.IdWorker;
//import com.changgou.web.CasUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：周
 * @ClassName: CommentServiceImpl
 * @date ：2019/8/15  14:44
 * @description ：
 * @version: 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private SkuFeign skuFeign ;
    @Autowired
    private UserFeign userFeign ;

    @Autowired
    private IdWorker idWorker;
    @Override
    @Transactional
    public void add(Map<String ,String> map) {
        if (map == null){
            return;
        }
        //商品适合度
        String conformity = map.get("conformity");
        //店家服务态度
        String manner = map.get("manner");
        //物流速度
        String speed = map.get("speed");
        //配送员服务
        String distribution = map.get("distribution");
        //商品评分
        String skuGrade = map.get("skuGrade");
        //skuId
        String skuId = map.get("skuId");
        //商品评价文本信息
        String commentText = map.get("comment");
        //用户晒图图片路径
        String skuImage = map.get("imageUrl");
        if(conformity == null){
            conformity = "5";
        }
        if (manner == null){
            manner ="5";
        }
        if (speed == null){
            manner ="5";
        }
        if (distribution == null){
            manner ="5";
        }
        if (skuGrade == null){
            manner ="5";
        }
        //根据skuid查询商品的spuid和规格
        Result result = skuFeign.findById(skuId);
        Sku sku = JSON.parseObject(JSON.toJSONString(result.getData()), Sku.class);

        //获取sku对应得spuid 和 规格
        String spuId = sku.getSpuId();
        String spec = sku.getSpec();
        if (spec == null){
            spec = "{'',''}";
        }
        Map<String,String> specMap = JSON.parseObject(spec,Map.class);
        List<String> specList = new ArrayList<>();
        for (String key : specMap.keySet()) {
            specList.add(key+":"+specMap.get(key));
        }
        String username = map.get("username");
        Result userResult = userFeign.findById(username);
        User user = JSON.parseObject(JSON.toJSONString(userResult.getData()), User.class);
        //获取用户昵称
        String nickName = user.getNickName();
        String headPic = user.getHeadPic();
        //封装数据到实体类
        Comment comment = new Comment();
        comment.setId(idWorker.nextId()+"");
        comment.setSpecList(specList);
        comment.setComment(commentText);
        comment.setConformity(Double.parseDouble(conformity));
        comment.setDistribution(Double.parseDouble(distribution));
        comment.setManner(Double.parseDouble(manner));
        comment.setSkuGrade(Double.parseDouble(skuGrade));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        comment.setPublishTime(date);
        comment.setSkuImage(skuImage);
        comment.setSpuId(spuId);
        comment.setSpeed(Double.parseDouble(speed));
        comment.setThumbup(0);
        comment.setCommentNum(0);
        comment.setNickName(nickName);
        comment.setUserImage(headPic);
        comment.setUserName(username);
        commentDao.save(comment);
    }

    @Override
    public Map query(String spuId) {

        Map resultMap = new HashMap();
        //List<Comment> commentList = commentDao.findAll();

        List<Comment> commentList = commentDao.findBySpuId(spuId);
        System.out.println(commentList);

        int zong = commentList.size();
        int hao = 0;
        int zhong = 0;
        int cha = 0 ;
        int imageNum = 0;

        for (Comment comment : commentList) {
            if (comment.getSkuGrade() >= 4.0 ){
                hao++;
            }
            if (comment.getSkuGrade() < 4.0 && comment.getSkuGrade() >= 3.0){
                zhong++;
            }
            if (comment.getSkuGrade() < 3.0){
                cha++;
            }
            if (comment.getSkuImage() != null){
                imageNum ++ ;
            }
        }
        resultMap.put("zong",zong);
        resultMap.put("hao",hao);
        resultMap.put("zhong",zhong);
        resultMap.put("cha",cha);
        resultMap.put("imageNum",imageNum);
        resultMap.put("commentList",commentList);
        return resultMap;
    }
}
